/*
@Time : 2018/10/15 10:59
@Author : LiM
@File : Youku
@Software: GoLand
*/
package spider

import (
	"github.com/PuerkitoBio/goquery"
	"github.com/tidwall/gjson"
	"log"
	"net/http"
	"strings"
)

type ChildVideoInfo struct {
	childName string
	childUrl  string
}

type VideoInfo struct {
	videoImage, videoUrl, videoName, videoType,
	videoYear, videoLanuage, videoSummary,
	videoOtherName string
	videoDirector []string
	videoCast     []string
	videoChild    []ChildVideoInfo
}

func youkuVideo(strUrl string) []VideoInfo {
	var youkuVideo []VideoInfo
	res, err := http.Get(strUrl)
	if err != nil {
		log.Fatal(err)
	}
	defer res.Body.Close()
	if res.StatusCode != 200 {
		log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
	}

	// Load the HTML document
	docFrame, errFrame := goquery.NewDocumentFromReader(res.Body)
	if errFrame != nil {
		log.Fatal(errFrame)
	}
	docFrame.Find("script").Each(func(iFrameIndex int, s *goquery.Selection) {
		var strContent string = s.Text()
		if strings.Contains(strContent, "bpmodule-main") && strings.Contains(strContent, "domid") {
			strContent = strings.Replace(strContent, "bigview.view", "", -1)
			strContent = string([]rune(strContent)[1 : len([]rune(strContent))-1])
			var strHtml string = gjson.Get(strContent, "html").String()
			docMain, errMain := goquery.NewDocumentFromReader(strings.NewReader(strHtml))
			if errMain != nil {
				log.Fatal(errMain)
			}
			docMain.Find(".sk-result-list .sk-mod").Each(func(iMainIndex int, selMain *goquery.Selection) {
				if selMain.Find(".mod-main .mod-play").Children().Length() > 0 {
					var videoInfo *VideoInfo = new(VideoInfo)
					var strVideoImage, strVideoUrl, strVideoName, strVideoType, strVideoYear, strVideoLanuage,
						strVideoSummary string
					strVideoImage, bImage := selMain.Find(".mod-left img").Attr("src")
					if !bImage {
						strVideoImage = ""
					}
					strVideoUrl, bUrl := selMain.Find(".mod-left a").Attr("href")
					if !bUrl {
						strVideoUrl = ""
					}
					strVideoName, bName := selMain.Find(".mod-main .mod-header h2.spc-lv-1 a").Attr("title")
					if !bName {
						strVideoName = ""
					}
					strVideoType = selMain.Find(".mod-main .mod-header .base-type").Text()
					strVideoYear = selMain.Find(".mod-main .mod-header span.spc-lv-1").Text()
					if selMain.Find(".mod-main .mod-header span").Length() > 2 {
						strVideoLanuage = selMain.Find(".mod-main .mod-header span").Last().Text()
					}
					strVideoSummary = selMain.Find(".mod-main .mod-info .row-ellipsis span").Text()

					videoInfo.videoImage = strVideoImage
					videoInfo.videoUrl = strVideoUrl
					videoInfo.videoName = strVideoName
					videoInfo.videoType = strVideoType
					videoInfo.videoYear = strVideoYear
					videoInfo.videoLanuage = strVideoLanuage
					videoInfo.videoSummary = strVideoSummary

					selMain.Find(".mod-main .mod-info span.spc-lv-4").Each(func(iDir int, selDir *goquery.Selection) {
						var strValue []string
						selDir.Find("a").Each(func(iA int, selA *goquery.Selection) {
							strValue = append(strValue, selA.Text())
						})

						if strings.Contains(selDir.Text(), "导演") {
							videoInfo.videoDirector = strValue
						} else if strings.Contains(selDir.Text(), "主演") {
							videoInfo.videoCast = strValue
						}
					})

					selMain.Find(".mod-play .mod-tab-wrap .mod-play-list li a").Each(func(iChild int, selChild *goquery.Selection) {
						var strChildName, strChildUrl string
						strChildName, bChildName := selChild.Attr("title")
						if !bChildName {
							strChildName = ""
						}

						strChildUrl, bChildUrl := selChild.Attr("href")
						if !bChildUrl {
							strChildUrl = ""
						}
						var childVideo *ChildVideoInfo = new(ChildVideoInfo)
						childVideo.childName = strChildName
						childVideo.childUrl = strChildUrl
						videoInfo.videoChild = append(videoInfo.videoChild, *childVideo)
					})
					youkuVideo = append(youkuVideo, *videoInfo)
				}

			})
		}
	})

	return youkuVideo
}
